package com.maktoub.apigateway.exception;

import com.maktoub.apigateway.common.BaseResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器
 *
 * @author yupi
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(BaseException.class)
    public BaseResponse<?> businessExceptionHandler(BaseException e) {
        log.error("BaseException: " + e.getMsg(), e);
        return new BaseResponse(e.getCode(),null,e.getMsg());
    }

    @ExceptionHandler(RuntimeException.class)
    public BaseResponse<?> runtimeExceptionHandler(RuntimeException e) {
        log.error("runtimeException", e);
        return new BaseResponse<>(500,null,e.getMessage());
    }
}
